﻿using ShoppingCar.head;
using ShoppingCar.Heads;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ShoppingCar
{
    public class HeadAppService : ApplicationService, IHeadService
    {
        private readonly IRepository<Headss, int> _heads;
        public HeadAppService(IRepository<Headss, int> heads)
        {
            _heads = heads;
        }
        //添加
        public int CreateHead(CreateUpdateHeadDto dto)
        {
            var add = new Headss()
            {
             
                Headheadline=dto.Headheadline,
                Headskip=dto.Headskip,
                HeadOrder=dto.HeadOrder,
                HeadState=dto.HeadState
            };
            _heads.InsertAsync(add);
            return 10;
        }
        //删除
        public int DeleteHead(int id)
        {
           
            _heads.DeleteAsync(id);
            //throw new NotImplementedException();
            return 1;
        }

        //显示
        public List<HeadListDto> GetHeadLists()
        {
            var get = from a in _heads
                      select new HeadListDto
                      {
                          Id = a.Id,
                          Headheadline = a.Headheadline,
                          Headskip = a.Headskip,
                          HeadOrder = a.HeadOrder,
                          HeadState = a.HeadState,
                          CreationTime = a.CreationTime                  
                      };
            var list = get.ToList();
            return list;
        }
        //反填
        public HeadDto Fantian(int id)
        {
            Headss headsss = _heads.Where(s => s.Id.Equals(id)).FirstOrDefault();
            return ObjectMapper.Map<Headss, HeadDto>(headsss);
        }
        //修改
        public int UpdateHead(int id,CreateUpdateHeadDto dto)
        {
            Headss headss = _heads.FirstOrDefault(s => s.Id.Equals(id));
            headss.Headheadline = dto.Headheadline;
            headss.Headskip = dto.Headskip;
            headss.HeadOrder = dto.HeadOrder;
            headss.HeadState = dto.HeadState;
            _heads.UpdateAsync(headss);
            return 10;
        }
        //按钮修改状态
        public HeadDto UpdateState(int id)
        {
            Headss bottom1 = _heads.FirstOrDefault(s => s.Id.Equals(id));
            if (bottom1.HeadState == 1)
            {
                bottom1.HeadState = 0;
            }
            else
            {
                bottom1.HeadState = 1;
            }
            return ObjectMapper.Map<Headss, HeadDto>(bottom1);
        }
    }
}
